package com.guigu.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.eduservice.dto.LoginResponseDto;
import com.guigu.eduservice.dto.OperatorDto;
import com.guigu.eduservice.entity.Operator;
import com.guigu.eduservice.entity.User;
import com.guigu.eduservice.mapper.OperatorMapper;
import com.guigu.eduservice.service.OperatorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.eduservice.utils.JwtUtils;
import com.guigu.eduservice.utils.MD5;
import com.guigu.eduservice.vo.OperatorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 操作员表（平台内部员工账号，如管理员、客服） 服务实现类
 * </p>
 *
 * @author janecraft
 * @since 2025-09-27
 */
@Service
public class OperatorServiceImpl extends ServiceImpl<OperatorMapper, Operator> implements OperatorService {
    @Autowired
    private OperatorService operatorService;




    @Override
    public LoginResponseDto login(OperatorDto dto) {
        LambdaQueryWrapper<Operator> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Operator::getLoginName, dto.getLoginname());
        Operator operator = this.getOne(wrapper);
        if (operator.getStatus() == 0)
            throw new RuntimeException("该用户已被禁用");
        if (!MD5.encrypt(dto.getPassword()).equals(operator.getPassword()))
            throw new RuntimeException("用户名或者密码错误");
        //登录成功
        String jwtToken = JwtUtils.getJwtToken(operator.getOperatorId(), operator.getName());
        // 封装用户信息
        OperatorDto operatorDto = new OperatorDto();
        operatorDto.setOperatorId(operator.getOperatorId());
        operatorDto.setLoginname(operator.getLoginName());
        operatorDto.setName(operator.getName());
        operatorDto.setPassword(operator.getPassword());
        operatorDto.setPhone(operator.getPhone());
        operatorDto.setStatus(operator.getStatus());

        LoginResponseDto response = new LoginResponseDto();
        response.setToken(jwtToken);
        response.setOperator(operatorDto);
        return response;
    }

    //操作员分页查询
    @Override
    public PageInfo<Operator> getOperatorAllPage(Integer PageNo, Integer PageSize, OperatorVo vo) {
        QueryWrapper<Operator> Wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(vo.getNameVo())){
            Wrapper.like("name",vo.getNameVo());
        }
        if(!StringUtils.isEmpty(vo.getPhoneVo())){
            Wrapper.like("phone",vo.getPhoneVo());
        }
        PageHelper.startPage(PageNo,PageSize);
        List<Operator> list = operatorService.list(Wrapper);
        PageInfo<Operator> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
